package com.controller.member;

import com.domain.Member;
import com.domain.Space;
import com.server.MemberService;
import com.server.SpaceService;
import com.server.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value="/member")
public class memberController {

    @Resource(name = "memberService")
    private MemberService memberService;

    @Resource(name = "userService")
    private UserService userService;

    @Resource(name = "spaceService")
    private SpaceService spaceService;

    //通用页面跳转控制器
    @RequestMapping(value = "/{formName}")
    public String f(@PathVariable String formName){
        return formName;
    }

    //通用页面跳转控制器
    @RequestMapping(value = "/space-member")
    public String findAllMember(@RequestParam(value="page",defaultValue="1") int page,
                                @RequestParam(value="pageSize",defaultValue="6") int pageSize,
                                HttpSession session, Model model){
        Space space= (Space) session.getAttribute("space");
        session.setAttribute("space",space);
        int space_id=space.getId();
        System.out.println("/space-member的***space_id==="+space_id);
        List<Member> memberList=memberService.findAllMember(page,pageSize,space_id);
        //计算总页数：
        int memberCount=memberService.findMemberTotal(space_id);
        System.out.println("(memberCount/pageSize)==="+(memberCount/pageSize));
        if((memberCount/pageSize)<1){
            model.addAttribute("pageTotal",1);
        }else {
            if (memberCount % pageSize != 0) {
                int pageTotal = new Integer(memberCount / pageSize + 1);
                model.addAttribute("pageTotal", pageTotal);
            } else {
                int pageTotal = new Integer(memberCount / pageSize);
                model.addAttribute("pageTotal", pageTotal);
            }
        }
        //设置当前为第一页：
        model.addAttribute("currentPage",page);
        model.addAttribute("memberList",memberList);
        model.addAttribute("memberCount",memberCount);
        return "space-member";
    }

    //通用页面跳转控制器
    @RequestMapping(value = "/selectMember")
    public String findMember(String username,
                             @RequestParam(value="page",defaultValue="1") int page,
                             @RequestParam(value="pageSize",defaultValue="6") int pageSize,
                             Model model,HttpSession session){
        System.out.println("username=="+username);
        model.addAttribute("username",username);
        Space space= (Space) session.getAttribute("space");
        int space_id=space.getId();
        List<Member> memberList=memberService.selectMemberByName(username,space_id);
        if(memberList.size()<1){
            String msg="暂无数据";
            model.addAttribute("msg",msg);
        }
        //计算总页数：
        int memberCount=memberService.findMemberTotal(space_id);
        if((memberCount/pageSize)<1){
            model.addAttribute("pageTotal",1);
        }else{
            if(memberCount%pageSize==1){
                int pageTotal=new Integer(memberCount/pageSize+1);
                model.addAttribute("pageTotal",pageTotal);
            }else{
                int pageTotal=new Integer(memberCount/pageSize);
                model.addAttribute("pageTotal",pageTotal);
            }
        }
        //设置当前为第一页：
        model.addAttribute("currentPage",page);
        model.addAttribute("memberList",memberList);
        model.addAttribute("memberCount",memberCount);
        return "space-member";
    }

    @ResponseBody
    @RequestMapping(value="/updateStatus",method = RequestMethod.POST)
    public void updateMemberStatus(int status,int member_id,HttpServletRequest request,
                                     HttpServletResponse response)throws Exception{
        System.out.println("需要更新的成员id==="+member_id);
        System.out.println("修改的成员状态status为："+status);
        String type = request.getHeader("X-Requested-With");
        if ("XMLHttpRequest".equals(type)) {
            //处理AJAX请求，设置响应头信息
            response.setHeader("REDIRECT", "REDIRECT");
            Member member=memberService.selectMemberById(member_id);
            member.setStatus(status);
            int i=memberService.updateMember(member);
            System.out.println("成功修改了"+i+"条成员状态");
            //需要跳转页面的URL
            response.setHeader("CONTEXTPATH", "space-member");
        } else {
            //其他的http请求，直接重定向就可以了
            response.sendRedirect("space-member");
        }
    }
}
